package com.lxl.exercise;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//给你一个整数数组 nums ，判断是否存在三元组 [nums[i], nums[j], nums[k]]
//满足 i != j、i != k 且 j != k ，同时还满足 nums[i] + nums[j] + nums[k] == 0
//。请你返回所有和为 0 且不重复的三元组。
public class Demo5 {
    public List<int[]> sum(int[] nums) {
        List<int[]> res = new ArrayList<int[]>();
        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j < nums.length; j++) {
                for (int k = j+1; k < nums.length; k++) {
                    if (nums[i] + nums[j] + nums[k] == 0) {
                        int[] temp = {nums[i], nums[j], nums[k]};
                        res.add(temp);
                    }
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Demo5 demo5 = new Demo5();
        List<int[]> sum = demo5.sum(new int[]{-1, 0, 1, 2, -1, -4});
        Arrays.toString(sum.toArray());
        System.out.println("sum = " + Arrays.deepToString(sum.toArray()));
    }
}
